/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.common.annotation.ServiceLog;
import org.springblade.common.constant.TrainingOperateConstant;
import org.springblade.common.utils.CommonUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.course.entity.CourseChapterEntity;
import org.springblade.modules.course.entity.CourseEntity;
import org.springblade.modules.course.service.ICourseChapterService;
import org.springblade.modules.course.service.ICourseService;
import org.springblade.modules.resource.enums.AttachType;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.course.entity.CourseChapterSubEntity;
import org.springblade.modules.course.vo.CourseChapterSubVO;
import org.springblade.modules.course.wrapper.CourseChapterSubWrapper;
import org.springblade.modules.course.service.ICourseChapterSubService;
import org.springblade.core.boot.ctrl.BladeController;

/**
 * 课程章节具体小节 控制器
 *
 * @author
 * @since 2023-03-22
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-courseChapterSub/courseChapterSub")
@Api(value = "课程章节具体小节", tags = "课程章节具体小节接口")
public class CourseChapterSubController extends BladeController {
	private final ICourseService courseService;
	private final ICourseChapterSubService courseChapterSubService;
	private final ICourseChapterService courseChapterService;
	private final CommonUtil commonUtil;
	/**
	 * 课程章节具体小节 详情
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入courseChapterSub")
	public R<CourseChapterSubVO> detail(CourseChapterSubEntity courseChapterSub) {
		CourseChapterSubEntity detail = courseChapterSubService.getOne(Condition.getQueryWrapper(courseChapterSub));
		Long subId=detail.getId();
		CourseChapterSubVO vo = CourseChapterSubWrapper.build().entityVO(detail);

		//设置小节章节名称
		CourseChapterEntity courseChapterEntity = courseChapterService.getById(detail.getChapterId());
		if (null!=courseChapterEntity)vo.setChapterName(courseChapterEntity.getChapterName());
		vo.setVedioLink(courseChapterSubService.queryBySubAndCategory(subId, AttachType.COURSE_VEDIO.getCategory()).get(0).getVedioLink());
		vo.setLearningCoursewareList(courseChapterSubService.queryCoursewareBySubAndCategory(subId, AttachType.COURSE_COURSEWARE.getCategory()).getLearningCoursewareList());
		return R.data(vo);
	}


	/**
	 * 课程章节具体小节 自定义分页
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/list")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入courseChapterSub")
	public R<IPage<CourseChapterSubVO>> page(CourseChapterSubVO courseChapterSub, Query query) {
		//不传分页字段表示获取下拉列表数据
		commonUtil.checkRoleAliasPermission(courseChapterSub,courseChapterSub.getClass());
		IPage<CourseChapterSubVO> pages = courseChapterSubService.selectCourseChapterSubPage(Condition.getPage(query), courseChapterSub);
		pages.getRecords().forEach(record->{
			CourseEntity courseEntity = courseService.getById(record.getCourseId());
			if (null!=courseEntity)record.setCourseName(courseEntity.getName());

			CourseChapterEntity courseChapterEntity = courseChapterService.getById(record.getChapterId());
			if (null!=courseChapterEntity)record.setChapterName(courseChapterEntity.getChapterName());
		});
		return R.data(pages);
	}

	/**
	 * 课程章节具体小节 新增
	 */
	@ServiceLog(value = TrainingOperateConstant.COURSE,operate = TrainingOperateConstant.ADD,describe = "章节小节新增")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入courseChapterSub")
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	public R save(@Valid @RequestBody CourseChapterSubVO vo) {
		CourseChapterEntity courseChapterEntity = courseChapterService.getById(vo.getChapterId());
		vo.setCourseId(courseChapterEntity.getCourseId());
		return R.status(courseChapterSubService.save(vo));
	}

	/**
	 * 课程章节具体小节 修改
	 */
	@ServiceLog(value = TrainingOperateConstant.COURSE,operate = TrainingOperateConstant.UPDATE,describe = "章节小节修改")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入courseChapterSub")
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	public R update(@Valid @RequestBody CourseChapterSubEntity courseChapterSub) {
		CourseChapterEntity courseChapterEntity = courseChapterService.getById(courseChapterSub.getChapterId());
		courseChapterSub.setCourseId(courseChapterEntity.getCourseId());
		return R.status(courseChapterSubService.updateById(courseChapterSub));
	}


	/**
	 * 课程章节具体小节 删除
	 */
	@ServiceLog(value = TrainingOperateConstant.COURSE,operate = TrainingOperateConstant.DELETE,describe = "章节小节删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(courseChapterSubService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 小节下拉接口
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/select")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "小节下拉接口", notes = "传入chapterId")
	public R courseSelect(Long chapterId) {
		QueryWrapper<CourseChapterSubEntity> queryWrapper =new QueryWrapper<>();
		if (null!=chapterId) queryWrapper.lambda().eq(CourseChapterSubEntity::getChapterId,chapterId);
		return R.data(courseChapterSubService.list(queryWrapper));
	}

}
